# This Python file uses the following encoding: utf-8
############################################################################
# Copyright (c) 2025 Li Auto Inc. and its affiliates
# Licensed under the Apache License, Version 2.0(the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
############################################################################
from openpyxl.styles import Alignment, Font, PatternFill, Border, Side


class ExcelUtils:
    @classmethod
    def write_merged_header(cls, sheet, header_info, add_str=""):
        font = Font(name="Cascadia Mono", size=12, color="FFFFFF", bold=True, italic=False, strike=False)
        border = Border(left=Side(border_style="thin", color="000000"),
                        right=Side(border_style="thin", color="000000"),
                        top=Side(border_style="thin", color="000000"),
                        bottom=Side(border_style="thin", color="000000"))
        alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
        for name, idx_list in header_info.items():
            cell = sheet.cell(row=idx_list[0], column=idx_list[2], value=name + add_str)
            cell.font = font
            cell.fill = PatternFill("solid", fgColor="3A5FCD")
            cell.border = border
            cell.alignment = alignment
            if idx_list[0] != idx_list[1] or idx_list[2] != idx_list[3]:
                sheet.merge_cells(start_row=idx_list[0],
                                  end_row=idx_list[1],
                                  start_column=idx_list[2],
                                  end_column=idx_list[3])

    @classmethod
    def write_sheet_cell(cls, sheet, row, column, value):
        cell = sheet.cell(row=row, column=column, value=value)
        cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)

    @classmethod
    def set_column_widths(cls, sheet, ori_length=0):

        for column in sheet.columns:
            max_length = ori_length
            for _ in column:
                if not hasattr(_, "column_letter"):
                    continue
                column_name = _.column_letter
                break
            for cell in column:
                try:
                    if len(str(cell.value)) > max_length:
                        max_length = len(cell.value)
                except Exception:
                    pass
            adjusted_width = (max_length + 2) * 1.2
            sheet.column_dimensions[column_name].width = adjusted_width

    @classmethod
    def set_row_heights(cls, sheet):
        for row in sheet.rows:
            row_height = 18
            for cell in row:
                try:
                    lines = str(cell.value).count('\n') + 1
                    adjusted_height = lines * 15
                    if adjusted_height > row_height:
                        row_height = adjusted_height
                except Exception:
                    pass
            sheet.row_dimensions[row[0].row].height = row_height

